const express = require("express");

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const expressSession = require("express-session");

app.use(
  expressSession({
    secret: "tools",
    resave: false,
    saveUninitialized: true,
  })
);

app.post("/api/user/login", (req, res) => {
  req.session.user = req.body;
  req.session.isLogin = true;
  res.send({
    code: 200,
    message: "登录成功",
  });
});

app.get("/api/user/info", (req, res) => {
  if (req.session.isLogin) {
    res.send({
      code: 200,
      message: "获取用户信息成功",
      data: req.session.user,
    });
  } else {
    res.send({
      code: 401, // 未授权
      message: "请先登录",
    });
  }
});

app.get("/api/user/logout", (req, res) => {
  req.session.destroy();
  res.send({
    code: 200,
    message: "退出登录成功",
  });
})

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});
